package com.chocho.producerserver.dao;

import com.chocho.producerserver.bean.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * Created by chocho on 2022/3/26.
 */
@Mapper
@Repository
public interface RoleDao {

    //根据用户Id获取角色列表
    @Select("SELECT role_id, role_name, role_status " +
            "FROM op_role LEFT JOIN op_user_role ON op_role.role_id = op_user_role.role_id " +
            "WHERE op_user_role.user_id = #{userId}")
    List<Role> selectRoleByUserId(String userId);

    @Select("SELECT MAX(role_order_num) FROM op_role")
    Integer getMaxNum();

    @Insert("INSERT INTO op_role " +
            "<trim prefix='(' suffix=')' suffixOverrides=',' > " +
            "<if test='roleId != null'>role_id, </if> " +
            "<if test='roleName != null' >role_name, </if>" +
            "<if test='roleStatus != null' >role_status, </if>" +
            "</trim> " +
            "<trim prefix='values (' suffix=')' suffixOverrides=',' > " +
            "<if test='roleId != null' > #{role_id,jdbcType=CHAR}, </if>" +
            "<if test='roleName != null' > #{role_name,jdbcType=VARCHAR}, </if>" +
            "<if test='roleStatus != null' > #{role_status,jdbcType=TINYINT}, </if>" +
            "</trim>")
    Integer insertNotNull(Role role);

    @Update("UPDATE op_role " +
            "<set> " +
            "<if test='roleId != null'> role_id = #{roleId,jdbcType=CHAR}, </if> " +
            "<if test='roleName != null'> role_name = #{roleName,jdbcType=VARCHAR}, </if> " +
            "<if test='roleStatus != null'> role_status = #{roleStatus,jdbcType=TINYINT}, </if> " +
            "</set> " +
            "WHERE role_id = #{roleId,jdbcType=CHAR} ")
    Integer updateByIDNotNull(Role role);

    @Delete("DELETE FROM op_role WHERE role_id = #{id}")
    Integer deleteByID(String id);

    @Select("SELECT * FROM op_role WHERE role_name like '%${name}%' ORDER BY role_order_num")
    List<Role> selectByLike(String name);

    @Select("SELECT * FROM op_role")
    List<Role> selectAll();
}
